home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / DCLAP 4j / Drtf / DPICTHandler.cpp < prev    next >
Encoding:
Text File  |  1995-12-17  |  6.7 KB  |  300 lines  |  [TEXT/R*ch]

  1. // DPICTHandler.cpp
  2. // d.g.gilbert
  3.  
  4.  
  5. #include "Dvibrant.h"
  6. #include "DFile.h"
  7. #include "DPICTHandler.h"
  8. #include "DPICTprocess.h"
  9. #include "DRichViewNu.h" //  DRichViewP
  10. #include "DDrawPICT.h"   // DDrawPICTst
  11.  
  12.  
  13.  
  14.  
  15. //class  DPICTHandler
  16.  
  17. DPICTHandler::DPICTHandler(DRichView* itsDoc, DFile* savefile) :
  18.     DRichHandler( itsDoc, savefile)
  19. {
  20.     fFormatName = "PICT";
  21.     fMinDataToProcess= 612;
  22.     if (fDocFile) fDocFile->Open("wb"); // make sure savefile is binary
  23. }
  24.  
  25. char* DPICTHandler::IsRich(char* buf, ulong buflen)
  26. {
  27.     DDrawPict* aDrawPict = new DDrawPict();
  28.     char* picat = (char*) aDrawPict->IsPICT(buf, buflen, (Nlm_Boolean)false/*dontTestForHead*/);
  29.     delete aDrawPict;
  30.     return picat;
  31. }
  32.  
  33. DRichprocess* DPICTHandler::NewProcessor( DFile* itsFile, Nlm_MonitorPtr progress)
  34. {
  35.         fProcessor = new DPICTprocess( itsFile, fDocFile, fDoc, progress);
  36.         return fProcessor;
  37. }
  38.  
  39. void DPICTHandler::InstallInStyle( DRichView* theDoc, DRichStyle* theStyle)
  40. {
  41.     if (fProcessor) {
  42.         ((DPICTprocess*)fProcessor)->InstallPicStyle(theStyle);  
  43.         delete fProcessor; 
  44.         fProcessor= NULL; 
  45.             // must search thru all paragraphs in theDoc and update item heights for
  46.         theDoc->UpdateStyleHeight(theStyle);
  47.         theDoc->SetAutoAdjust(true); 
  48.         //theDoc->AdjustScroll();
  49.         }  
  50. }
  51.  
  52.  
  53. Boolean DPICTHandler::ProcessData( char* cbeg, char* cend, Boolean endOfData, 
  54.                     ulong& dataRemaining) 
  55. {
  56.  
  57.     if (Format() == kPICTformat && fProcessStage == kAtStart && fDocFile) {
  58.             // write empty pict header !?
  59.         char zerobuf[512];
  60.         ulong count= 512;
  61.         Nlm_MemFill( zerobuf, 0, count);
  62.         fDocFile->WriteData( zerobuf, count);
  63.         fProcessStage= kAfterHeader;
  64.         }
  65.  
  66. #if 1            
  67.     if (!fProcessor) { 
  68.         fDoc->SetAutoAdjust(false);
  69.         fProcessor= this->NewProcessor((DFile*)NULL,(Nlm_MonitorPtr)NULL); 
  70.         fProcessStage = kInBody;
  71.         }
  72.  
  73.     if (dataRemaining && fDocFile) {
  74.         ulong count= dataRemaining;  
  75.         fDocFile->WriteData( cbeg, count);
  76.         dataRemaining= 0;
  77.         }
  78.         
  79.     if (endOfData && fProcessor) { 
  80.         if (fDocFile) fDocFile->Close();
  81.     
  82.         switch (fTasknum) {
  83.         
  84.             case 1: // inline image
  85.                 // fProcessor->InstallPicStyle(theStyle); is called by others
  86.                 break;
  87.                 
  88.             default: 
  89.                 fProcessor->Close(); 
  90.                 if (fProcessor->GetTitle()) fDoc->SetTitle( (char*)fProcessor->GetTitle());
  91.                 delete fProcessor; 
  92.                 fProcessor= NULL; 
  93.                 fDoc->AdjustScroll();
  94.                 fDoc->SetAutoAdjust(true);   
  95.                 break;
  96.             }
  97.             
  98.         return true;
  99.         } 
  100.     return 3; //<< flag update date but not view; // true;
  101.     
  102. #else    
  103.  
  104.             // Dgg_DrawPict is not currently written to draw partial PICT data
  105.             // So for now, this handler should always return false until endOfData is true
  106.     if (!endOfData) return false;
  107.  
  108.     return DRichHandler::ProcessData( cbeg, cend, endOfData, dataRemaining);        
  109. #endif
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. //class  DPICTprocess
  120.  
  121.  
  122. DPICTprocess::DPICTprocess( DFile* itsFile, DFile* itsTempData, 
  123.                                         DRichView* itsDoc, Nlm_MonitorPtr progress) :
  124.         DRichprocess( itsFile, itsDoc, progress),
  125.         fPictMax(0), fPictSize(0), fPict(NULL), 
  126.         fTempData( itsTempData),
  127.         fNotDone(true), fInPic(true) 
  128. {     
  129. }
  130.     
  131.  
  132. DPICTprocess::~DPICTprocess()
  133. {
  134.     //////// nooooo //// Nlm_MemFree(fPict); // data is passed to displayer
  135. }
  136.  
  137. void DPICTprocess::Close()
  138. {
  139.     (void) PutPicture();
  140. }
  141.  
  142. void DPICTprocess::Read()
  143. {
  144.     (void) PutPicture();
  145. }
  146.  
  147.  
  148.  
  149. void DPICTprocess::PutPicChar(short c)
  150. {
  151.     if (1 + fPictSize >= fPictMax) {
  152.         fPictMax = fPictSize + 1025;
  153.         if (!fPict) fPict= (char*) Nlm_MemNew(fPictMax);
  154.         else fPict= (char*) Nlm_MemMore( fPict, fPictMax);
  155.         }
  156.     if (fPict) fPict[fPictSize++]= (char) c;
  157. }
  158.  
  159.  
  160. void DPICTprocess::handleTextClass()
  161. {
  162.     if (fInPic) {
  163.         PutPicChar( fMajor);
  164.         }
  165.     else {
  166.         if (IsNewStyle()) StoreStyle(fOldStyleRec.style, FALSE);  
  167.         PutLitChar(fMinor); 
  168.         fLastChar= fMajor; //??
  169.         }
  170. }
  171.  
  172. void DPICTprocess::handleControlClass()
  173. {
  174.     if (fInPic) 
  175.         PutPicChar( fMinor);
  176.     else  
  177.         DRichprocess::handleControlClass();
  178. }
  179.  
  180.  
  181. Boolean DPICTprocess::StuffInStyle( DRichStyle* theStyle)
  182. {            
  183.     if (fPictSize) {
  184.         fPict[fPictSize]= 0;
  185.         DPictStyle* newPict= 
  186.                 new DPictStyle( DPictStyle::kPictMac, fPict, fPictSize, true);  
  187.         Nlm_RecT picrect;
  188.         DDrawPict* aDrawPict = new DDrawPict();
  189.         aDrawPict->GetFrame( &picrect, fPict, fPictSize, DDrawPict::kPictMac);
  190.         delete aDrawPict;
  191.         theStyle->pixwidth= picrect.right - picrect.left;
  192.         theStyle->pixheight= picrect.bottom - picrect.top;
  193.  
  194.         DPictStyle* oldPict= theStyle->fPict;
  195.         if (oldPict) {
  196.             Nlm_RecT loc;
  197.             // netpict info we need to keep ?!
  198.             // this is an html pict!?, reset loc for netlinks 
  199.             Boolean resetLoc= (oldPict->fLinks[0].fKind == DPictStyle::kPictNetPic);
  200.             short i, n = oldPict->fNumLinks;
  201.             for (i= 0; i<n; i++) {
  202.                 if (resetLoc) loc = picrect;
  203.                 else loc= oldPict->fLinks[i].fLoc;
  204.                 if (oldPict->fLinks[i].fKind == DPictStyle::kPictNetLink
  205.                  || oldPict->fLinks[i].fKind == DPictStyle::kPictMapLink)
  206.                     newPict->AddLink( oldPict->fLinks[i].fKind, oldPict->fLinks[i].fLinkid, loc);
  207.                 }
  208.             oldPict->suicide(); // ?? delete
  209.             }
  210.         theStyle->ispict= TRUE;
  211.         theStyle->fPict= newPict;
  212.         
  213.         fPictSize= 0;
  214.         fPict= NULL; //??
  215.         return true;
  216.         }
  217.     else
  218.         return false;
  219. }
  220.  
  221. void DPICTprocess::ReadDataFromFile()
  222. {    
  223.     if (!fDataFile) fDataFile= fTempData;
  224.     if (fNotDone && fDataFile) {
  225.         ulong filelen, fileindex;
  226.         fDataFile->Close(); // !?!? are we done writing to fTempData ??
  227.         fDataFile->Open("rb"); 
  228.         fDataFile->GetDataLength( filelen);
  229.         
  230.         fPictMax= filelen+1;
  231.         fPict= (char*) Nlm_MemNew(fPictMax);        
  232.  
  233.             //// need to skip MacDraw 512 byte header, if it exists 
  234.         fPictSize= MIN(filelen, 1024);
  235.         fDataFile->ReadData( fPict, fPictSize);
  236.         DDrawPict* aDrawPict = new DDrawPict();
  237.         char* picat = (char*) aDrawPict->IsPICT( fPict, fPictSize, false/*dontTestForHead*/);
  238.         delete aDrawPict;
  239.         fileindex= picat - fPict;
  240.         fDataFile->SetDataMark( fileindex);
  241.         fPictSize= filelen - fileindex;
  242.         fDataFile->ReadData( fPict, fPictSize);
  243.         }
  244. }            
  245.         
  246.  
  247. Boolean DPICTprocess::StuffInDocument()
  248. {    
  249.     if (fPictSize) {
  250.         //PutLitStr("\n");  
  251.         //NewParagraph();
  252.  
  253.         (void) StuffInStyle( &fStyleRec.style);
  254.  
  255.         PutLitChar(' '); // (chPictTag); /* put a space for some real text ? */
  256.         StoreStyle(fStyleRec.style, TRUE);
  257.  
  258.         fPictSize= 0;
  259.         fStyleRec.style.fPict= NULL;
  260.         fStyleRec.style.ispict= FALSE;
  261.         fStyleRec.style.pixwidth= 0; 
  262.         fStyleRec.style.pixheight= 0; 
  263.         fOldStyleRec= fStyleRec;
  264.         fOldStyleRec.fontname= NULL; /* ?? force new style ? */
  265.         fInPic= FALSE;
  266.  
  267.         StoreStyle(fStyleRec.style, TRUE);
  268.         PutLitChar('\n'); /* put a space for some real text ? */
  269.         NewParagraph();
  270.  
  271.         //PutLitStr("\n");  
  272.         //NewParagraph();
  273.         return true;
  274.         }
  275.     else
  276.         return false;
  277. }
  278.  
  279.             
  280. Boolean DPICTprocess::InstallPicStyle( DRichStyle* theStyle)
  281. {    
  282.     ReadDataFromFile();
  283.     if (fPictSize && theStyle)  
  284.         return StuffInStyle( theStyle);
  285.     else
  286.         return false;
  287. }
  288.  
  289. Boolean DPICTprocess::PutPicture()
  290. {    
  291.     ReadDataFromFile();
  292.     if (fPictSize)  
  293.         return StuffInDocument();
  294.     else
  295.         return false;
  296. }
  297.  
  298.  
  299.  
  300.